
$(function(){
	getUserRole();
	
});
/*公共部分js加载*/
function getUserRole(){
	/*var role=0;//用户角色，0-管理员，1-客户；
	var urlstr=window.location.href;
	urlstr=urlstr.replace(/\?.*$/,'');
	urlstr=urlstr.replace(/^.*\//,'');
	if(navUrl[role].indexOf(urlstr)>=0){
		navdata=navData[role];
		getNavShow(navdata);
	}else{
		consoleMes($("#manageContent"),2444000,"warning","你无权限查看！");//success、info、warning、danger
    	setTimeout(function () {
	    	window.location.href=gobalWeb+"login.html";
	    }, 2500);
	}*/
	$.post(
        gobalServer+"user/checkLogin",
		{},
        function(backData){
            if(backData.statusCode==200){
                /*var role=0;
                var urlstr=window.location.href;
                urlstr=urlstr.replace(/\?.*$/,'');
                urlstr=urlstr.replace(/^.*\//,'');
                if(navUrl[role].indexOf(urlstr)>=0){
                    navdata=navData[role];
                    getNavShow(navdata);
                }else{
                    consoleMes($("#manageContent"),2000,"warning","你无权限查看！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"login.html";
                    }, 2500);
                }*/
            	if(backData.object!=null){
                    /*获取键值：localStorage.getItem(“key”)
                    设置键值：localStorage.setItem(“key”,”value”)
                    清除键值：localStorage.removeItem(“key”)
                    清除所有键值：localStorage.clear()*/
                    localStorage.setItem("userID",backData.object.userId);
                    localStorage.setItem("userRole",backData.object.permission);

            		var role=null;
                    if(backData.object.permission==7001){
                        role=0;
					}else if(backData.object.permission==7002){
                        role=1;
					}
                    var urlstr=window.location.href;
                    urlstr=urlstr.replace(/\?.*$/,'');
                    urlstr=urlstr.replace(/^.*\//,'');
                    if(navUrl[role].indexOf(urlstr)>=0){
                        navdata=navData[role];
                        getNavShow(navdata);
                    }else{
                        consoleMes($("#manageContent"),2000,"warning","你无权限查看！");//success、info、warning、danger
                        setTimeout(function () {
                            window.location.href=gobalWeb+"login.html";
                        }, 2500);
                    }
				}else{
                    consoleMes($("#manageContent"),2000,"warning","你无权限查看！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"login.html";
                    }, 2500);
				}

            }else if(backData.statusCode==-20){
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                setTimeout(function () {
                    window.location.href=gobalWeb+"login.html";
                }, 2500);
			}else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
	);
};


/*非公共部分js加载，仅首页界面*/
function getShowMes(type){
	/*indexStatisticsShow(datarow);*/
	if(type){
		var getUrl="";
		if(type==1){
            getUrl=gobalServer+"order/getChartByTimeYears"
		}else if(type==2){
            getUrl=gobalServer+"order/getChartByTimeMonths"
        }else if(type==3){
            getUrl=gobalServer+"order/getChartByTimeDays"
        }
        $.post(
            getUrl,
            {},
            function(backData){
                if(backData.statusCode==200){
                   $("#statisticsList").show();
                    indexStatisticsShow(backData);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );
	}else{
		consoleMes($("#manageContent"),3000,"warning","请选择盘点类型");//success、info、warning、danger
	}

};
function indexStatisticsShow(datarow){
	if(datarow!=null){
		$("#totalNum").text(datarow.totalCount);
		$("#totalMoney").text(datarow.totalPrice);
		/*显示表格*/
		statisticsEchart(datarow);
	}else{
		consoleMes($("#manageContent"),3000,"warning","无法获取统计数据，请刷新重试！");
	}
};
function statisticsEchart(datarow){
	if(datarow!=null){
		
		var xdata=[];
		var ydata=[];
		$.each(datarow.chartByCount, function(i,item) {
			xdata.push(item.xParam);
			ydata.push(item.yParam);
		});
        echartShow('echartShow1','库存出货量统计','出货量（件）',xdata,ydata);
        xdata=[];
        ydata=[];
        $.each(datarow.chartByPrice, function(i,item) {
            xdata.push(item.xParam);
            ydata.push(item.yParam);
        });
        echartShow('echartShow2','库存销售额统计','销售额（元）',xdata,ydata);
	}else{
		consoleMes($("#manageContent"),3000,"warning","无法相关数据，统计图表无法加载，请刷新重试！");
	}
};
/*表格显示*/
function echartShow(IDStr,title,legendstr,xdata,ydata){
	// 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById(IDStr));

	var colors = ['#5793f3', '#d14a61', '#675bba'];	
	
	option = {
	    title: {
	        text: title
	    },
	    tooltip: {
	        trigger: 'axis'
	    },
	    legend: {
	        data:[legendstr]
	    },
	    grid: {
	        left: '3%',
	        right: '4%',
	        bottom: '3%',
	        containLabel: true
	    },
	    toolbox: {
	        feature: {
	            saveAsImage: {}
	        }
	    },
	    xAxis: {
	        type: 'category',
	        boundaryGap: false,
	        data: xdata
	    },
	    yAxis: {
	        type: 'value'
	    },
	    series: [
	        {
	            name:legendstr,
	            type:'line',
	            stack: '总量',
	            data:ydata
	        }
	    ]
	};

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
};
